public class BabyUnicorn extends Thread
{
	
	static Barrier playing;
	String status;
	static Semaphore eatingSem2;
	int babyId;
	static Semaphore eatingSem;
	
	
	public BabyUnicorn(int babyId) {
		BabyUnicorn.playing = new Barrier();
		eatingSem			= new Semaphore(1);
		eatingSem2 			= new Semaphore(1);
		status = "Ready-to-play";
		this.babyId = babyId;
		this.start();
	}
	
	public void run() {
		try {
			play();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized int getBabyId() {
		return babyId;
	}
	
	public synchronized void  play() throws InterruptedException {
		Activities.getReadyToPlay().add(this);
		playing.block();
        status = "Playing";
		System.out.println("baby " + this.getBabyId() + " is " + status);
		this.wait(3000);
		Activities.getReadyToPlay().remove(this);
		
		this.eating();
	 }

	public synchronized void eating() throws InterruptedException {
		eatingSem.acquire();

		if (Activities.getFullPots() > 0) {
		int fullPots = Activities.getFullPots();
		fullPots--;
		Activities.setFullPots(fullPots);
		Activities.getEating().add(this);
		Activities.getHungry().remove(this);
		status = "Eating";
		System.out.println("baby with id " + this.getBabyId() + "is"
		+ status);
		// wait

		Activities.getReadyToPlay().add(this);
		Activities.getEating().remove(this);

		if (Activities.getHungry().size() > 0
		&& Activities.getEating().size() == 0) {
		System.out.println("print");
		Activities.sleepyMummy.release();
		}

		}
		eatingSem.release();

		if (status.compareTo("Eating") == 0) {
		play();
		}

		else {
		eatingSem2.acquire(); 
		if (Activities.getFullPots() == 0) {

		System.out.println("baby with id " + this.getBabyId()
		+ "found full pots =0");
		Activities.getHungry().add(this);

		if (Activities.getEating().size() == 0
		&& Activities.getHungry().size() == 1) {  

		System.out.println("baby with id " + this.getBabyId()
		+ "found that no one is eating ");
		wakingMama(this);
		Activities.getHungry().remove(this);
		eatingSem2.release();
		eating();
		} else {
		System.out.println("baby with id" + this.getBabyId()
		+ "enetered the else part");
		eatingSem2.release();

		Activities.getWaitingToEat().acquire();
		Activities.getHungry().remove(this);
		eating();

		}
		} else {  
		eatingSem2.release();
		eating();
		}
		}
		}
	
	public synchronized void wakingMama(BabyUnicorn baby) throws InterruptedException 
	{
		System.out.println("baby "+this.getBabyId()+" went to wake mummy");
		Activities.sleepyMummy.release();
		System.out.println("baby "+this.getBabyId()+" 2ana da5el 2anam");
		//
		Activities.getWaitingToEat().acquire();
		//System.out.println("baby with id"+this.getBabyId()+"2ana da5el 2anam2");
		
	}


	public Barrier getPlaying() {
		return playing;
	}

	public void setPlaying(Barrier playing) {
		this.playing = playing;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Semaphore getEatingSem() {
		return eatingSem;
	}

	public void setEatingSem(Semaphore eatingSem) {
		this.eatingSem = eatingSem;
	}

	public Semaphore getEatingSem2() {
		return eatingSem2;
	}

	public void setEatingSem2(Semaphore eatingSem2) {
		this.eatingSem2 = eatingSem2;
	}
	
	public void setBabyId(int babyId) {
		this.babyId = babyId;
	}

}
